package com.wlcj.www.aop;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.Cookie;

import org.apache.commons.collections.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;
import com.wlcj.constant.Global;
import com.wlcj.plugin.NutzPlugin;
import com.wlcj.www.annotation.LoginRequired;
import com.wlcj.www.enumtype.DomainEnum;
import com.wlcj.www.model.data.Attention;
import com.wlcj.www.model.data.Data;
import com.wlcj.www.model.data.PlatformAttention;
import com.wlcj.www.service.AttentionService;
import com.wlcj.www.service.DataService;
import com.wlcj.www.service.IndexService;
import com.wlcj.www.util.EncryptionUtil;

public class LoginInterceptor implements Interceptor {
	
	private IndexService indexService = NutzPlugin.getBean(IndexService.class);

	private  AttentionService attentionService = NutzPlugin.getBean(AttentionService.class);
	
	private  DataService dataService = NutzPlugin.getBean(DataService.class);
	@Override
	public void intercept(ActionInvocation ai) {
		System.out.println("登陆拦截器...");
		
		LoginRequired l = ai.getMethod().getAnnotation(LoginRequired.class);
		Controller controller = ai.getController();
		Cookie cookie = controller.getCookieObject(Global.LOGIN_COOKIE_NAME);
		if(cookie==null) { 
			controller.removeSessionAttr(Global.SESSION_LOGIN_KEY);
			controller.removeSessionAttr(Global.SESSION_LOGIN_NAME_KEY);
			if(l!=null)
				controller.redirect(Global.LOGIN_URL+"?returnUrl="+Global.DEFAULT_RETURN_URL);
			else
				ai.invoke();
		} else {
			String loginStr = cookie.getValue();
			JSONObject loginJson = JSON.parseObject(loginStr);
			String code = loginJson.getString("code");
			String timestamp = loginJson.getString("timestamp");
			int uid = loginJson.getInteger("uid");
			String key = DomainEnum.KEY.getDesc();
			String code2 = EncryptionUtil.toHexString(EncryptionUtil.md5(uid+timestamp+key));
			if(code.equals(code2)) {
				System.err.println("登陆通过...");
				String userName = indexService.getLoginNameById(uid);
				controller.setSessionAttr(Global.SESSION_LOGIN_KEY, uid);
				controller.setSessionAttr(Global.SESSION_LOGIN_NAME_KEY, userName);
				//登录成功取cookie的平台加入关注列表
				savePlatform(ai,uid);
				ai.invoke();
			} else {
				System.err.println("登陆不通过...");
				controller.removeSessionAttr(Global.SESSION_LOGIN_KEY);
				controller.removeSessionAttr(Global.SESSION_LOGIN_NAME_KEY);
				if(l!=null)
					controller.redirect(Global.LOGIN_URL+"?returnUrl="+Global.DEFAULT_RETURN_URL);
				else
					ai.invoke();
			}
		}
	}

	public void savePlatform(ActionInvocation ai,int uid){
		// 判断cookie是否有值
		Controller controller = ai.getController();
		Cookie cookie = controller.getCookieObject("the_cookie");
		try {
			if(cookie!=null){
				List<Data> data = Data.dao.getMaxDate();
				String time = data.get(0).getDate("data_date").toString();
				// 判断关注列表有没有值
				List<PlatformAttention> att = attentionService.platform(uid, time);
				String the_cookie = URLDecoder.decode(cookie.getValue(),"UTF-8");
				System.out.println(the_cookie);
				try{
					List<Attention> list = JSON.parseArray(the_cookie,Attention.class);
					
					//[{"pid":"11","pname":"投哪网"},{"pid":"51","pname":"微贷网"},{"pid":"56","pname":"PPmoney"},{"pid":"95","pname":"宜人贷"},{"pid":"6","pname":"红岭创投"}]
					if(CollectionUtils.isEmpty(att)){
						for(int i=0;i<list.size();i++){
							if(list.get(i).getIssubmit()==1){
								dataService.addUserAttentionPlatform(uid, list.get(i).getPid());
								}
							}
						}
				}catch(Exception e){
					System.err.print("类型转换错误:"+the_cookie);
				}
	        	
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
